//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package javax.vpncrypto;

import java.nio.ByteBuffer;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.ProviderException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;

public abstract class CipherSpi {
    public CipherSpi() {
    }

    protected abstract void engineSetMode(String var1) throws NoSuchAlgorithmException;

    protected abstract void engineSetPadding(String var1) throws NoSuchPaddingException;

    protected abstract int engineGetBlockSize();

    protected abstract int engineGetOutputSize(int var1);

    protected abstract byte[] engineGetIV();

    protected abstract AlgorithmParameters engineGetParameters();

    protected abstract void engineInit(int var1, Key var2, SecureRandom var3) throws InvalidKeyException;

    protected abstract void engineInit(int var1, Key var2, AlgorithmParameterSpec var3, SecureRandom var4) throws InvalidKeyException, InvalidAlgorithmParameterException;

    protected abstract void engineInit(int var1, Key var2, AlgorithmParameters var3, SecureRandom var4) throws InvalidKeyException, InvalidAlgorithmParameterException;

    protected abstract byte[] engineUpdate(byte[] var1, int var2, int var3);

    protected abstract int engineUpdate(byte[] var1, int var2, int var3, byte[] var4, int var5) throws javax.vpncrypto.ShortBufferException;

    protected int engineUpdate(ByteBuffer var1, ByteBuffer var2) throws javax.vpncrypto.ShortBufferException {
        try {
            return this.bufferCrypt(var1, var2, true);
        } catch (javax.vpncrypto.IllegalBlockSizeException var4) {
            throw new ProviderException("Internal error in update()");
        } catch (javax.vpncrypto.BadPaddingException var5) {
            throw new ProviderException("Internal error in update()");
        }
    }

    protected abstract byte[] engineDoFinal(byte[] var1, int var2, int var3) throws javax.vpncrypto.IllegalBlockSizeException, javax.vpncrypto.BadPaddingException;

    protected abstract int engineDoFinal(byte[] var1, int var2, int var3, byte[] var4, int var5) throws javax.vpncrypto.ShortBufferException, javax.vpncrypto.IllegalBlockSizeException, javax.vpncrypto.BadPaddingException;

    protected int engineDoFinal(ByteBuffer var1, ByteBuffer var2) throws javax.vpncrypto.ShortBufferException, javax.vpncrypto.IllegalBlockSizeException, javax.vpncrypto.BadPaddingException {
        return this.bufferCrypt(var1, var2, false);
    }

    static int getTempArraySize(int var0) {
        return Math.min(4096, var0);
    }

    private int bufferCrypt(ByteBuffer var1, ByteBuffer var2, boolean var3) throws javax.vpncrypto.ShortBufferException, javax.vpncrypto.IllegalBlockSizeException, BadPaddingException {
        if (var1 != null && var2 != null) {
            int var4 = var1.position();
            int var5 = var1.limit();
            int var6 = var5 - var4;
            if (var3 && var6 == 0) {
                return 0;
            } else {
                int var7 = this.engineGetOutputSize(var6);
                if (var2.remaining() < var7) {
                    throw new ShortBufferException("Need at least " + var7 + " bytes of space in output buffer");
                } else {
                    boolean var8 = var1.hasArray();
                    boolean var9 = var2.hasArray();
                    int var10 = 0;
                    byte[] var11;
                    byte[] var12;
                    int var13;
                    if (var9) {
                        var12 = var2.array();
                        var13 = var2.position();
                        int var14 = var2.arrayOffset() + var13;
                        int var15;
                        if (var8) {
                            var11 = var1.array();
                            var15 = var1.arrayOffset() + var4;
                            if (var3) {
                                var10 = this.engineUpdate(var11, var15, var6, var12, var14);
                            } else {
                                var10 = this.engineDoFinal(var11, var15, var6, var12, var14);
                            }

                            var1.position(var5);
                        } else {
                            var11 = new byte[getTempArraySize(var6)];

                            do {
                                var15 = Math.min(var6, var11.length);
                                if (var15 > 0) {
                                    var1.get(var11, 0, var15);
                                }

                                int var16;
                                if (!var3 && var6 <= var15) {
                                    var16 = this.engineDoFinal(var11, 0, var15, var12, var14);
                                } else {
                                    var16 = this.engineUpdate(var11, 0, var15, var12, var14);
                                }

                                var10 += var16;
                                var14 += var16;
                                var6 -= var15;
                            } while(var6 > 0);
                        }

                        var2.position(var13 + var10);
                    } else if (var8) {
                        var11 = var1.array();
                        var13 = var1.arrayOffset() + var4;
                        if (var3) {
                            var12 = this.engineUpdate(var11, var13, var6);
                        } else {
                            var12 = this.engineDoFinal(var11, var13, var6);
                        }

                        var1.position(var5);
                        if (var12 != null && var12.length != 0) {
                            var2.put(var12);
                            var10 = var12.length;
                        }
                    } else {
                        var11 = new byte[getTempArraySize(var6)];

                        do {
                            var13 = Math.min(var6, var11.length);
                            if (var13 > 0) {
                                var1.get(var11, 0, var13);
                            }

                            if (!var3 && var6 <= var13) {
                                var12 = this.engineDoFinal(var11, 0, var13);
                            } else {
                                var12 = this.engineUpdate(var11, 0, var13);
                            }

                            if (var12 != null && var12.length != 0) {
                                var2.put(var12);
                                var10 += var12.length;
                            }

                            var6 -= var13;
                        } while(var6 > 0);
                    }

                    return var10;
                }
            }
        } else {
            throw new NullPointerException("Input and output buffers must not be null");
        }
    }

    protected byte[] engineWrap(Key var1) throws IllegalBlockSizeException, InvalidKeyException {
        throw new UnsupportedOperationException();
    }

    protected Key engineUnwrap(byte[] var1, String var2, int var3) throws InvalidKeyException, NoSuchAlgorithmException {
        throw new UnsupportedOperationException();
    }

    protected int engineGetKeySize(Key var1) throws InvalidKeyException {
        throw new UnsupportedOperationException();
    }

    protected void engineUpdateAAD(byte[] var1, int var2, int var3) {
        throw new UnsupportedOperationException("The underlying Cipher implementation does not support this method");
    }

    protected void engineUpdateAAD(ByteBuffer var1) {
        throw new UnsupportedOperationException("The underlying Cipher implementation does not support this method");
    }
}
